package com.cl.it.spider;

import com.cl.it.mbg.entity.Company;
import com.cl.it.mbg.entity.News;
import com.cl.it.utils.DateUtils;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import lombok.extern.slf4j.Slf4j;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

@Component
@Slf4j
public class IfengSpider extends AbstractSpider {

    @Override
    protected void executeSingleCompany(WebDriver driver, WebDriverWait wait, Company company) {
        String url = "https://so.ifeng.com/?q=###&c=1";
        url = url.replace("###", company.getShortName());
        driver.get(url);

        String listCssSelector = "ul.news-stream-basic-news-list>li.news_item h2>a";
        if (!has(driver, listCssSelector)) {
            return;
        }
        List<WebElement> articles = selectors(driver, listCssSelector);
        if (articles == null || articles.size() == 0) {
            return;
        }
        List<String> articleUrls = new ArrayList<>();
        for (int i = 0; i < articles.size(); i++) {
            WebElement element = articles.get(i);
            articleUrls.add(element.getAttribute("href"));
        }

        for (String articleUrl : articleUrls) {
            driver.navigate().to(articleUrl);
            News news = new News();
            news.setDataSource("凤凰科技");
            news.setName(company.getName());
            if (has(driver, "div[class^=containerBox-]")) {
                news.setTitle(selectorText(driver, "div[class^=containerBox-]>h2"));
                news.setNewsDate(DateUtils.dateConvert(selectorText(driver, "div[class^=containerBox-] div[class^=info-] time")));
                news.setSource(selectorText(driver, "div[class^=containerBox-] div[class^=infobox-] span[class^=source-]"));
                news.setSourceUrl(articleUrl);

                int plen = selectors(driver, "div[class^=containerBox-] div[class^=detailBox-] div[class^=smallFont-] p").size();
                List<WebElement> ps = selectors(driver, "div[class^=containerBox-] div[class^=detailBox-] div[class^=smallFont-] p");
                int index = 0;
                String content = null;
                do {
                    String p = ps.get(index).getText().trim();
                    if (null != p && !"".equals(p) && p.length() >= 20) {
                        content = p;
                        break;
                    }
                } while (index ++ < plen);
                news.setContent(content);

            } else if (has(driver, "div[class^=leftContent-]")) {

                news.setTitle(selectorText(driver, "div[class^=leftContent-] div[class^=artical-] h1[class^=topic-]"));
                news.setNewsDate(DateUtils.dateConvert(selectorText(driver, "div[class^=leftContent-] div[class^=artical-] div[class^=info-] p[class^=time-] span:nth-child(1)")));
                news.setSource(selectorText(driver, "div[class^=leftContent-] div[class^=artical-] div[class^=info-] p[class^=time-] span[class^=source-]"));
                news.setSourceUrl(articleUrl);

                int plen = selectors(driver, "div[class^=leftContent-] div[class^=artical-] div[class^=main_content-] div[class^=text-] p").size();
                List<WebElement> ps = selectors(driver, "div[class^=leftContent-] div[class^=artical-] div[class^=main_content-] div[class^=text-] p");
                int index = 0;
                String content = null;
                do {
                    String p = ps.get(index).getText().trim();
                    if (null != p && !"".equals(p) && p.length() >= 20) {
                        content = p;
                        break;
                    }
                } while (index ++ < plen);
                news.setContent(content);
            } else {

            }

            UpdateWrapper<News> updateWrapper = new UpdateWrapper<>();
            updateWrapper.eq("title", news.getTitle());
            updateWrapper.eq("name", news.getName());
            newsService.saveOrUpdate(news, updateWrapper);

        }


    }


    @Override
    protected void login(WebDriver driver, WebDriverWait wait) {

    }
}
